<?php

require_once 'classes/LightPressPlugin.php';

class PostCommentDebug extends LightPressPlugin {
    
    var $constructor_args = array('dir'=>'Where to save the dumped comments');
    var $default_context = LP_CONTEXT_POST;
    var $description = 'Save a copy of all comments for debugging purposes.';
    
    var $dir = '/tmp/';
    
    var $active = true;
    var $hooks = array('insert_comment');
    
    function PostCommentDebug(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        if (!is_dir($this->dir) || !is_writable($this->dir))
            $this->active = false;
    }
    
    function run($hook, &$payload) {
        $dump = $payload;
        $filename = $this->dir . $this->_frontend->options['shortname'] . '_' . strftime("%Y%m%d%H%M%S") . '_' . uniqid();
        $text = '';
        foreach ($dump as $k=>$v)
            $text .= "$k = $v\n";
        $text .= "\n-------------- GET -------------\n\n";
        foreach ($_GET as $k=>$v)
            $text .= "$k = $v\n";
        $text .= "\n-------------- POST -------------\n\n";
        foreach ($_POST as $k=>$v)
            $text .= "$k = $v\n";
        $text .= "\n-------------- SERVER -------------\n\n";
        foreach ($_SERVER as $k=>$v)
            $text .= "$k = $v\n";
        $text .= "\n-------------- COOKIE -------------\n\n";
        foreach ($_COOKIE as $k=>$v)
            $text .= "$k = $v\n";
        $f = @fopen("$filename.txt", 'wb');
        @fwrite($f, $text);
        @fclose($f);
        $dump['_GET'] = $_GET;
        $dump['_POST'] = $_POST;
        $dump['_SERVER'] = $_SERVER;
        $dump['_COOKIE'] = $_COOKIE;
        $f = @fopen("$filename.dump", 'wb');
        @fwrite($f, serialize($payload));
        @fclose($f);
    }
    
}

?>